<?php

namespace app\goods\controller;

use app\BaseController;
use app\category\model\Category;
use app\model\Goods;

class Index extends BaseController
{
    /**
     * 根据id获取商品的信息
     *
     * @return Array
     */
    public function index()
    {
        $goodsId = $this->request->param('id');
        $DBgoods = new Goods();
        $goodsInfo = $DBgoods->where('goods_id', $goodsId)->field('goods_id,goods_name,goods_desc,goods_picture,cid,goods_price,goods_discount,attribute_list')->find()->toArray();
        $goodsInfo['goods_old_price'] = rand(2, 8) + $goodsInfo['goods_price'];
        if ($goodsInfo) {
            // 不为空  查询父级商品名称
            $DBcategory = new Category();
            $categoryInfo = $DBcategory->where('children_id', $goodsInfo['cid'])->field('children_name,children_id')->find()->toArray();
            $pInfo = $DBcategory->where('children_id', $categoryInfo['children_id'])->find()->toArray();
            $p = $DBcategory->where('id', $pInfo['pid'])->where('name', '<>', '')->field('id,name')->find()->toArray();
            return result(array_merge($categoryInfo, $p, $goodsInfo));
        }
        return result(null, '获取信息失败', 403);
    }
    public function find()
    {
        $page = $this->request->param('page');
        $DBGoods = new Goods();
        $info = $DBGoods->page($page, 11)->select();
        $count = $DBGoods->count();
        return result(['sub' => $info, 'count' => $count]);
    }
}